package com.qyhl.webtv.module_news.news.union.town2;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.cloud.webtv.module_news.R;
import com.qyhl.cloud.webtv.module_news.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.UnionBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by qyhl on 2018/2/26.
 */

public class Town2ListFragment extends BaseFragment {

    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;

    private CommonAdapter mAdapter;

    private List<UnionBean> mDataList = new ArrayList<>();

    public void setmDataList(List<UnionBean> mDataList) {
        this.mDataList = mDataList;
    }


    public static Town2ListFragment newInstance(List<UnionBean> dataList) {
        Town2ListFragment fragment = new Town2ListFragment();
        fragment.setmDataList(dataList);
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.news_fragment_town2_list, container, false);
    }

    @Override
    protected void create() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));

        mAdapter = new CommonAdapter<UnionBean>(getContext(), R.layout.news_item_town2_list, mDataList) {
            @Override
            protected void convert(ViewHolder holder, UnionBean unionBean, int position) {
                ImageView logo = holder.getView(R.id.logo_icon);
                TextView title = holder.getView(R.id.title);

                title.setText(unionBean.getName());
                Glide.with(getContext())
                        .load(unionBean.getLogo())
                        .apply(new RequestOptions().error(R.drawable.cover_square_default).placeholder(R.drawable.cover_square_default))
                        .into(logo);
            }
        };

        EmptyWrapper emptyWrapper = new EmptyWrapper(mAdapter);
        emptyWrapper.setEmptyView(R.layout.news_layout_common_emptyview);
        mRecyclerView.setAdapter(emptyWrapper);
    }

    @Override
    protected void setListener() {
        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", mDataList.get(position).getId());
                bundle.putString("title", mDataList.get(position).getName());
                bundle.putString("cover", mDataList.get(position).getLogo());
                RouterManager.router(ARouterPathConstant.DETAIL_TOWN,bundle);

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public void initImmersionBar() {
    }



    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }


}
